#include "HelloWorldScene.h"
#include "MenuScene.h"
#include "SimpleAudioEngine.h"
#include "Header.h"
using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /////////////////////////////
    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("logo.png");
    pSprite->setScaleX(SIZE.width/pSprite->getContentSize().width);
    pSprite->setScaleY(SIZE.height/pSprite->getContentSize().height);
    pSprite->setOpacity(0);
    // position the sprite on the center of the screen
    pSprite->setPosition( ccp(SIZE.width/2, SIZE.height/2) );

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    CCFadeIn* fadeIn = CCFadeIn::create(1.5);
    CCDelayTime* delay = CCDelayTime::create(0.5);
    CCCallFuncO* callChange = CCCallFuncO::create(this, callfuncO_selector(HelloWorld::menuCloseCallback), NULL);
    pSprite->runAction(CCSequence::create(fadeIn, delay,callChange,NULL));
    
    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    removeAllChildrenWithCleanup(true);
    CCTextureCache::sharedTextureCache()->removeAllTextures();
    CCSpriteFrameCache::sharedSpriteFrameCache()->purgeSharedSpriteFrameCache();
    CCDirector::sharedDirector()->replaceScene(MenuScene::scene());
}
